#!/bin/bash

source  /opt/edp/${service.serviceName}/conf/install-iceberg.sh

<#assign ramPercentage = conf['flink.hs.jvm.memory.percentage']?number>

export FLINK_HS_HEAP=$[ $MEM_LIMIT / 1024 / 1024  * ${ramPercentage} / 100 ]M
export FLINK_ENV_JAVA_OPTS_HS="-Xmx$FLINK_HS_HEAP -Xms$FLINK_HS_HEAP $FLINK_ENV_JAVA_OPTS_HS"

export FLINK_ENV_JAVA_OPTS_HS="$FLINK_ENV_JAVA_OPTS_HS  -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=9925 -javaagent:/opt/jmx_exporter/jmx_prometheus_javaagent-0.14.0.jar=5555:/opt/edp/${service.serviceName}/conf/jmx_prometheus.yaml -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/opt/edp/${service.serviceName}/log/gc-flink-yarnserver.log"

export FLINK_APP_ID=`yarn app -list|grep "Apache Flink"|awk '{print $1}'`

if [ -n "$FLINK_APP_ID" ]; then
  echo "Flink Application is running, please stop it first"
  yarn app -kill $FLINK_APP_ID
fi

nohup ${r"${FLINK_HOME}"}/bin/yarn-session.sh > /dev/null 2>&1 &

while true; do
  sleep 5
  export FLINK_APP_OD=`yarn app -list|grep "Apache Flink"|awk '{print $1}'`
  if [ -n "$FLINK_APP_OD" ]; then
    echo "Flink Application is running"
    echo "running" > /opt/edp/${service.serviceName}/log/flink-yarnserver.pid
    break
  fi
done

tail -f /dev/null